import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Label;



public class register extends Dialog {

	protected Object result;
	protected Shell shell;
   protected ClientLogic logic;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public register(Shell parent, int style) {
		super(parent, style);
		setText("Register");
      logic = new ClientLogic();
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	protected void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 270);
		shell.setText(getText());
		
		Label lblUserName = new Label(shell, SWT.NONE);
		lblUserName.setBounds(10, 20, 91, 17);
		lblUserName.setText("User Name:");		
		
		final Label availabilityLabel = new Label(shell, SWT.NONE);
		availabilityLabel.setBounds(273, 53, 165, 17);
		availabilityLabel.setVisible(false);
		
		final Label resultLabel = new Label(shell, SWT.NONE);
		resultLabel.setBounds(137, 171, 301, 17);
		resultLabel.setVisible(false);
		
		final Text UserName = new Text(shell, SWT.BORDER);
		UserName.setBounds(137, 10, 301, 27);
		
		Label lblPassword = new Label(shell, SWT.NONE);
		lblPassword.setBounds(10, 102, 81, 17);
		lblPassword.setText("Password:");
		
		final Text Password = new Text(shell, SWT.BORDER);
		Password.setBounds(137, 92, 301, 27);
		
		final Text RetypePassword = new Text(shell, SWT.BORDER);
		RetypePassword.setBounds(137, 138, 301, 27);
		
		Label label = new Label(shell, SWT.NONE);
		label.setText("Retype Password:");
		label.setBounds(10, 148, 127, 17);
		
		Button btnCancel = new Button(shell, SWT.NONE);
		btnCancel.setText("Cancel");
		btnCancel.setBounds(263, 194, 91, 29);
		btnCancel.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				shell.close();
			}
		});
		/*
		Button btnCheckAvailability = new Button(shell, SWT.NONE);
		btnCheckAvailability.setText("Check Availability");
		btnCheckAvailability.setBounds(137, 43, 130, 29);
		btnCheckAvailability.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				// check if user name exists
				if (logic.exists(UserName.getText())
        {
             availabilityLabel.setText("Username Unavailable");
        }
        else
        {
             availabilityLabel.setText("Username Available");
        }
        availabilityLabel.setVisible(true);
			}
		});*/
		
		Button btnRegister = new Button(shell, SWT.NONE);
		btnRegister.setBounds(84, 194, 91, 29);
		btnRegister.setText("Register");
		btnRegister.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				if (UserName.getText().equals("")||Password.getText().equals("")/*|| RetypePassword.getText().equals("")*/) {
					resultLabel.setText("No fields can be blank.");
				} else if (!Password.getText().equals(RetypePassword.getText())) {
					resultLabel.setText("The password fields don't match.");
				} else
					try {
						if (logic.createUser(UserName.getText(), Password.getText(), "IP Address"))//need to get current Ip address and pass it to ClientLogic
      {
							new friendlist().open();
         shell.close();
      } else {
         resultLabel.setText("This username already exists.");
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
        resultLabel.setVisible(true);   
			}
		});
	}
}